package utils

import (
	"context"
	"fmt"
	"net/http"
	"product/common/enumeration"
)

type Response[T any] struct {
	Status  int    `json:"Status"`
	Message string `json:"Message"`
	Data    T      `json:"Data,omitempty"`
}

// Success 成功
func Success(data any, msg string,
	status enumeration.ProcessingStatus) *Response[any] {
	return &Response[any]{
		Status:  int(status),
		Message: msg,
		Data:    data,
	}
}

func Fail(msg string) *Response[any] {
	return &Response[any]{
		Status:  int(enumeration.ProcessingFailed),
		Message: msg,
		Data:    nil,
	}
}

// Error 处理错误
func Error(err string) *Response[any] {
	return &Response[any]{
		Status:  int(enumeration.ProcessingError),
		Message: err,
		Data:    nil,
	}
}

// ErrorHandler 统一错误处理
func ErrorHandler(name string) func(ctx context.Context,
	err error) (int, any) {
	return func(ctx context.Context,
		err error) (int, any) {
		return http.StatusOK,
			Error(fmt.Sprintf("%s", err))
	}
}
